package com.atguigu.controller;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 步骤1：实现HandlerInterceptor接口
 * 步骤2：在SpringMVC配置文件中注册这个拦截器的工作
 *      配置拦截器，拦截哪些请求的目标方法：
 *          因为【一个请求】，对应【一个处理器】中的【一个方法】
 *
 * @author 伟哥
 * @date 2021-05-26 10:40
 */
public class MyFirstInterceptor implements HandlerInterceptor {
    /**
     * 目标方法之前运行。
     *      return true，放行
     *      return false，拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        System.out.println("MyFirstInterceptor的preHandle方法...");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyFirstInterceptor的postHandle方法...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyFirstInterceptor的afterCompletion方法...");
    }
}
